import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _isNil from "lodash/isNil";
import _find from "lodash/find";
import _isFunction from "lodash/isFunction";
import { postProcessComponentProps, // 公共的组件属性 后处理
defaultPostPropProcess } from "../utils";
import { fieldNameProp } from "../config"; // 后处理组件属性 Post 只能改变 props 下级及 field 非 props 下级 的值。

export function postDynamicFormProcessComponentProps(_ref) {
  var _ref$field = _ref.field,
      field = _ref$field === void 0 ? {} : _ref$field,
      dealProps = _ref.dealProps,
      dataSource = _ref.dataSource,
      form = _ref.form,
      fields = _ref.fields;
  var postPropProcess = postPropProcesses[field.componentType];

  if (postPropProcess) {
    return postPropProcess({
      field: field,
      dealProps: dealProps,
      dataSource: dataSource,
      form: form,
      fields: fields
    });
  } else {
    return defaultPostPropProcess(dealProps);
  }
} // 表单

export var postPropProcesses = {
  ValueList: postValueListPropProcess,
  Lov: postLovPropProcess
};
/**
 * 处理级联
 * @param {Object} field - 需要处理级联的字段
 * @param {Object} props - 经过处理过的属性
 * @param {Object} form - 表单
 * @param {Object[]} fields - 所有的字段
 */

function processForCascade(_ref2) {
  var field = _ref2.field,
      form = _ref2.form,
      fields = _ref2.fields,
      props = _ref2.props;
  var cascadeFlag = props.cascadeFlag,
      cascadeField = props.cascadeField,
      cascadeFrom = props.cascadeFrom,
      queryParams = props.queryParams;
  delete props.cascadeField;
  delete props.cascadeFrom;
  delete props.cascadeFlag;

  if (cascadeFlag && cascadeFrom && cascadeField) {
    // 如果是 级联 且 级联字段为 null 或者 undefined 则设置为 disabled
    props.disabled = _isNil(form.getFieldValue(cascadeFrom));

    if (queryParams) {
      props.queryParams = function () {
        var q;

        if (_isFunction(queryParams)) {
          q = queryParams();
        } else {
          q = _objectSpread({}, queryParams);
        }

        var cascadeValue = form.getFieldValue(cascadeFrom);
        q[cascadeField] = cascadeValue;
        return q;
      };
    } else {
      props.queryParams = function () {
        var cascadeValue = form.getFieldValue(cascadeFrom);
        return _defineProperty({}, cascadeField, cascadeValue);
      };
    }

    var cascadeF = _find(fields, function (f) {
      return f[fieldNameProp] === cascadeFrom;
    });

    var cascadeTo = field[fieldNameProp];

    if (cascadeF && cascadeF.props) {
      var cascadeFProps = cascadeF.props;

      if (_isFunction(cascadeFProps.onChange)) {
        var c = cascadeFProps.onChange;

        cascadeFProps.onChange = function () {
          c();
          form.setFieldsValue(_defineProperty({}, cascadeTo, undefined));
        };
      } else {
        cascadeFProps.onChange = function () {
          form.setFieldsValue(_defineProperty({}, cascadeTo, undefined));
        };
      }
    }
  }
}

function postValueListPropProcess(_ref4) {
  var field = _ref4.field,
      dealProps = _ref4.dealProps,
      form = _ref4.form,
      dataSource = _ref4.dataSource,
      fields = _ref4.fields;
  var props = postProcessComponentProps({
    field: field,
    componentType: 'ValueList',
    dealProps: dealProps,
    dataSource: dataSource
  }); // 处理 ValueList 级连

  processForCascade({
    field: field,
    form: form,
    fields: fields,
    props: props
  });
  return props;
}

function postLovPropProcess(_ref5) {
  var field = _ref5.field,
      dealProps = _ref5.dealProps,
      form = _ref5.form,
      dataSource = _ref5.dataSource,
      fields = _ref5.fields;
  var props = postProcessComponentProps({
    field: field,
    componentType: 'Lov',
    dealProps: dealProps,
    dataSource: dataSource
  }); // 处理 Lov 级连

  processForCascade({
    field: field,
    form: form,
    fields: fields,
    props: props
  });
  return props;
}